<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="imlgw,半岛铁盒,blog,Java博客,程序员,个人博客,java開發,程序員,個人博客,Java">
    <meta name="description" content="大悲无泪，大悟无言，大笑无声。">
    <meta name="author" content="Resolmi">
    
    <title>
        
            可以获取最小值的栈 |
        
        Tadow
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    <link rel="shortcut icon" href="https://static.imlgw.top/blog/20210731/BtJz541CcmJU.ico">
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/css/font-awesome.min.css">
    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"imlgw.top","root":"/","language":"zh-CN","path":"search.json"};
    KEEP.theme_config = {"toc":{"enable":true,"number":true,"expand_all":true,"init_open":true},"style":{"primary_color":"#0066CC","avatar":"https://static.imlgw.top/blog/20210731/3C7hCSRR3lfq.png","favicon":"https://static.imlgw.top/blog/20210731/BtJz541CcmJU.ico","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":false,"scale":true},"first_screen":{"enable":true,"background_img":"/images/image.svg","description":"Keep It Simple & Stupid."},"scroll":{"progress_bar":{"enable":true},"percent":{"enable":true}}},"local_search":{"enable":true,"preload":false},"code_copy":{"enable":true,"style":"default"},"pjax":{"enable":true},"lazyload":{"enable":true},"version":"3.4.3"};
    KEEP.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 月前","year":"%s 年前"};
  </script>
<meta name="generator" content="Hexo 5.4.0"><link rel="stylesheet" href="/css/prism.css" type="text/css"></head>


<body>
<div class="progress-bar-container">
    
        <span class="scroll-progress-bar"></span>
    

    
        <span class="pjax-progress-bar"></span>
        <span class="pjax-progress-icon">
            <i class="fas fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                Tadow
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/"
                            >
                                首页
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/archives"
                            >
                                归档
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/categories"
                            >
                                分类
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/sbe"
                            >
                                订阅
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/links"
                            >
                                友链
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/about"
                            >
                                关于
                            </a>
                        </li>
                    
                    
                        <li class="menu-item search search-popup-trigger">
                            <i class="fas fa-search"></i>
                        </li>
                    
                </ul>
            </div>
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fas fa-search"></i></div>
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/">首页</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/archives">归档</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/categories">分类</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/sbe">订阅</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/links">友链</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/about">关于</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">可以获取最小值的栈</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="https://static.imlgw.top/blog/20210731/3C7hCSRR3lfq.png">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">Resolmi</span>
                        
                            <span class="author-label">BOSS</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;2018-09-27 00:00:00
    </span>
    
        <span class="article-categories article-meta-item">
            <i class="fas fa-folder"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/categories/%E7%AE%97%E6%B3%95/">算法</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fas fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/%E7%AE%97%E6%B3%95/">算法</a>&nbsp;
                    </li>
                
                    <li>
                        | <a href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
        <span class="article-wordcount article-meta-item">
            <i class="fas fa-file-word"></i>&nbsp;<span>1.7k 字</span>
        </span>
    
    
        <span class="article-min2read article-meta-item">
            <i class="fas fa-clock"></i>&nbsp;<span>7 分钟</span>
        </span>
    
    
        <span class="article-pv article-meta-item">
            <i class="fas fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <h2 id="如何实现可以获取最小值的栈？"><a href="#如何实现可以获取最小值的栈？" class="headerlink" title="如何实现可以获取最小值的栈？"></a>如何实现可以获取最小值的栈？</h2><p>这个问题是很久之前在微信公众号上看见的一个问题，突然想起来就来操作一下。<br>开发环境   : sublime+MinGW<br>先附上我自己实现的栈的结构</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//定义链栈的节点的结构体 </span></span><br><span class="line">typedef struct  StackNode</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">int</span> data;</span><br><span class="line">	struct StackNode *next;</span><br><span class="line">&#125;StackNode,*LinkStackPtr;</span><br><span class="line"></span><br><span class="line"><span class="comment">//链栈的结构体</span></span><br><span class="line">typedef struct LinkStack</span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">//栈顶</span></span><br><span class="line">	LinkStackPtr top;</span><br><span class="line">	<span class="comment">//总数量</span></span><br><span class="line">	<span class="keyword">int</span> count;</span><br><span class="line">&#125;LinkStack;</span><br><span class="line"></span><br><span class="line"><span class="comment">//初始化栈</span></span><br><span class="line">LinkStack *init()&#123;</span><br><span class="line">	LinkStack *stack=(LinkStack*)malloc(sizeof(LinkStack));</span><br><span class="line">	<span class="keyword">if</span> (stack==NULL)</span><br><span class="line">	&#123;</span><br><span class="line">		printf(<span class="string">&quot;动态开辟空间失败&quot;</span>);</span><br><span class="line">	&#125;<span class="keyword">else</span></span><br><span class="line">		<span class="comment">//初始化空栈</span></span><br><span class="line">		stack-&gt;top=NULL;</span><br><span class="line">		stack-&gt;count=<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">return</span> stack;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//创建节点</span></span><br><span class="line"><span class="function">LinkStackPtr  <span class="title">creatNode</span><span class="params">()</span></span>&#123;</span><br><span class="line">	LinkStackPtr stack;</span><br><span class="line">	<span class="comment">//开辟空间</span></span><br><span class="line">	stack =(LinkStackPtr)malloc(sizeof(StackNode));</span><br><span class="line">	<span class="keyword">if</span>(stack==NULL)&#123;</span><br><span class="line">		printf(<span class="string">&quot;动态开辟空间失败&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	scanf(<span class="string">&quot;%d&quot;</span>,&amp;(stack-&gt;data));</span><br><span class="line">	stack-&gt;next=NULL;</span><br><span class="line">	<span class="keyword">return</span> stack;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//压栈</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">push</span><span class="params">(LinkStack * s)</span></span>&#123;</span><br><span class="line">	<span class="comment">//创建节点</span></span><br><span class="line">	LinkStackPtr newStack= creatNode();</span><br><span class="line">	<span class="comment">//保存当前的节点</span></span><br><span class="line">	LinkStackPtr currentTopStack=s-&gt;top;</span><br><span class="line">	<span class="comment">//栈顶变为刚进栈的元素</span></span><br><span class="line">	s-&gt;top=newStack;</span><br><span class="line">	<span class="comment">//连接</span></span><br><span class="line">	newStack-&gt;next=currentTopStack;</span><br><span class="line">	<span class="comment">//数目加一</span></span><br><span class="line">	s-&gt;count++;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//弹栈</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">pop</span><span class="params">(LinkStack *s)</span></span>&#123;</span><br><span class="line">	<span class="comment">//将栈顶弹出</span></span><br><span class="line">	LinkStackPtr topStack=s-&gt;top;</span><br><span class="line">	s-&gt;top=topStack-&gt;next;</span><br><span class="line">	s-&gt;count--;</span><br><span class="line">	free(topStack);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="Solution-1："><a href="#Solution-1：" class="headerlink" title="Solution 1："></a><strong>Solution 1</strong>：</h3><p>在进栈的时候用一个变量保存当前的最小值每次进栈就会和最小值比较如果比最小值要小就会更新这个变量的值，出栈的时候比较麻烦，如果最小值被弹出去了就需要遍历整个栈来获取最小值。</p>
<ul>
<li>Implement：</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">typedef struct  StackNode</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">int</span> data;</span><br><span class="line">	struct StackNode *next;</span><br><span class="line">&#125;StackNode,*LinkStackPtr;</span><br><span class="line"></span><br><span class="line"><span class="comment">//链栈的结构体</span></span><br><span class="line">typedef struct LinkStack</span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">//栈顶</span></span><br><span class="line">	LinkStackPtr top;</span><br><span class="line">	<span class="comment">//总数量</span></span><br><span class="line">	<span class="keyword">int</span> count;</span><br><span class="line">	<span class="comment">//最小值</span></span><br><span class="line">	<span class="keyword">int</span> min;</span><br><span class="line">&#125;LinkStack;</span><br><span class="line"></span><br><span class="line"><span class="comment">//进栈</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">push</span><span class="params">(LinkStack * s)</span></span>&#123;</span><br><span class="line">	<span class="comment">//创建节点</span></span><br><span class="line">	LinkStackPtr newStack= creatNode();</span><br><span class="line">	<span class="comment">//**********************************</span></span><br><span class="line">	<span class="keyword">if</span>(s-&gt;count==<span class="number">0</span>)&#123;</span><br><span class="line">		s-&gt;min=newStack-&gt;data;	</span><br><span class="line">	&#125;<span class="keyword">else</span> <span class="keyword">if</span>(s-&gt;min&gt;newStack-&gt;data)&#123;</span><br><span class="line">		s-&gt;min=newStack-&gt;data;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//*********************************</span></span><br><span class="line">	<span class="comment">//保存当前的节点</span></span><br><span class="line">	LinkStackPtr currentTopStack=s-&gt;top;</span><br><span class="line">	<span class="comment">//栈顶变为刚进栈的元素</span></span><br><span class="line">	s-&gt;top=newStack;</span><br><span class="line">	<span class="comment">//连接</span></span><br><span class="line">	newStack-&gt;next=currentTopStack;</span><br><span class="line">	<span class="comment">//数目加一</span></span><br><span class="line">	s-&gt;count++;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//弹栈</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">pop</span><span class="params">(LinkStack *s)</span></span>&#123;</span><br><span class="line">	<span class="comment">//将栈顶弹出</span></span><br><span class="line">	LinkStackPtr topStack=s-&gt;top;</span><br><span class="line">	<span class="comment">//从第二个开始比较</span></span><br><span class="line">	LinkStackPtr stackNode=topStack-&gt;next;</span><br><span class="line">	<span class="comment">//弹出去的那个节点的数据</span></span><br><span class="line">	<span class="keyword">int</span> min=topStack-&gt;data;</span><br><span class="line">	s-&gt;top=topStack-&gt;next;</span><br><span class="line">	s-&gt;count--;</span><br><span class="line">	free(topStack);</span><br><span class="line">	<span class="comment">//如果弹出的是最小值就需要从当前栈顶遍历数组获取最小值</span></span><br><span class="line">	<span class="keyword">if</span>(min==s-&gt;min)&#123;</span><br><span class="line">		min=stackNode-&gt;data;</span><br><span class="line">	  <span class="keyword">do</span>&#123;</span><br><span class="line">	  	<span class="keyword">if</span>(min&gt;stackNode-&gt;data)&#123;</span><br><span class="line">	  		min=stackNode-&gt;data;</span><br><span class="line">	  	&#125;</span><br><span class="line">		stackNode=stackNode-&gt;next;</span><br><span class="line">	   &#125;<span class="keyword">while</span>(stackNode!=NULL);	</span><br><span class="line">	&#125;</span><br><span class="line">	s-&gt;min=min;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这种方法进栈时间复杂度为O(1),但是出栈时间复杂度为O(n).显然不是很优雅。</p>
<h3 id="Solution-2："><a href="#Solution-2：" class="headerlink" title="Solution 2："></a><strong>Solution 2</strong>：</h3><p>空间换时间利用一个辅助栈，辅助栈里面存放最小值，辅助栈进栈时判断进栈的元素和当前栈顶的元素大小跟小就可以进栈。所以最小值就是辅助栈的栈顶元素，出栈时如果出栈的元素是最小值节点那辅助栈也同时弹栈 再取栈顶元素。</p>
<ul>
<li>Implement：</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">#include &lt;stdio.h&gt;</span><br><span class="line">#include &lt;stdlib.h&gt;</span><br><span class="line">#include &lt;string.h&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义链栈的节点的结构体 </span></span><br><span class="line">typedef struct  StackNode</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">int</span> data;</span><br><span class="line">	struct StackNode *next;</span><br><span class="line">&#125;StackNode,*LinkStackPtr;</span><br><span class="line"></span><br><span class="line"><span class="comment">//链栈的结构体</span></span><br><span class="line">typedef struct LinkStack</span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">//栈顶</span></span><br><span class="line">	LinkStackPtr top;</span><br><span class="line">	<span class="comment">//总数量</span></span><br><span class="line">	<span class="keyword">int</span> count;</span><br><span class="line">	<span class="comment">//辅助栈</span></span><br><span class="line">	struct LinkStack *mins;</span><br><span class="line">&#125;LinkStack;</span><br><span class="line"></span><br><span class="line"><span class="comment">//初始化栈</span></span><br><span class="line">LinkStack *init()&#123;</span><br><span class="line">	LinkStack *stack=(LinkStack*)malloc(sizeof(LinkStack));</span><br><span class="line">	<span class="keyword">if</span> (stack==NULL)</span><br><span class="line">	&#123;</span><br><span class="line">		printf(<span class="string">&quot;动态开辟空间失败&quot;</span>);</span><br><span class="line">	&#125;<span class="keyword">else</span></span><br><span class="line">		<span class="comment">//初始化空栈</span></span><br><span class="line">		stack-&gt;top=NULL;</span><br><span class="line">		stack-&gt;count=<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">return</span> stack;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//创建节点</span></span><br><span class="line"><span class="function">LinkStackPtr  <span class="title">creatNode</span><span class="params">()</span></span>&#123;</span><br><span class="line">	LinkStackPtr stack;</span><br><span class="line">	<span class="comment">//开辟空间</span></span><br><span class="line">	stack =(LinkStackPtr)malloc(sizeof(StackNode));</span><br><span class="line">	<span class="keyword">if</span>(stack==NULL)&#123;</span><br><span class="line">		printf(<span class="string">&quot;动态开辟空间失败&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	scanf(<span class="string">&quot;%d&quot;</span>,&amp;(stack-&gt;data));</span><br><span class="line">	stack-&gt;next=NULL;</span><br><span class="line">	<span class="keyword">return</span> stack;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//进栈</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">push</span><span class="params">(LinkStack * s)</span></span>&#123;</span><br><span class="line">	<span class="comment">//创建节点</span></span><br><span class="line">	LinkStackPtr newStack= creatNode();</span><br><span class="line">	<span class="comment">//保存当前的节点</span></span><br><span class="line">	LinkStackPtr currentTopStack=s-&gt;top;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//开辟空间 用于添加到辅助栈中，之前直接操作newStack，指针地址导致后面辅助栈和主栈混合到了一起，，，，</span></span><br><span class="line">	LinkStackPtr minStackNode =(LinkStackPtr)malloc(sizeof(StackNode));</span><br><span class="line">	<span class="keyword">if</span>(minStackNode==NULL)&#123;</span><br><span class="line">		printf(<span class="string">&quot;动态开辟空间失败&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	minStackNode-&gt;data=newStack-&gt;data;</span><br><span class="line">	minStackNode-&gt;next=NULL;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//栈顶变为刚进栈的元素</span></span><br><span class="line">	s-&gt;top=newStack;</span><br><span class="line">	<span class="comment">//辅助栈</span></span><br><span class="line">	LinkStack *mins=s-&gt;mins;</span><br><span class="line">	<span class="comment">//保存辅助栈当前栈顶</span></span><br><span class="line">	LinkStackPtr  currentMinsTop=mins-&gt;top;</span><br><span class="line">	<span class="keyword">if</span>(s-&gt;count==<span class="number">0</span>)&#123;</span><br><span class="line">	   mins-&gt;top=newStack;</span><br><span class="line">	   newStack-&gt;next=NULL;</span><br><span class="line">    &#125;<span class="keyword">else</span></span><br><span class="line">    	<span class="comment">//如果进栈的元素比当前辅助栈的栈顶小</span></span><br><span class="line">    	<span class="comment">//相等的要进栈</span></span><br><span class="line">    	<span class="keyword">if</span>(newStack-&gt;data&lt;=mins-&gt;top-&gt;data)&#123;</span><br><span class="line">    		mins-&gt;top=minStackNode;</span><br><span class="line">    		<span class="comment">//连接</span></span><br><span class="line">    		mins-&gt;top-&gt;next=currentMinsTop;</span><br><span class="line">    		mins-&gt;count++;</span><br><span class="line">    	&#125;</span><br><span class="line">	<span class="comment">//连接</span></span><br><span class="line">	s-&gt;top-&gt;next=currentTopStack;</span><br><span class="line">	<span class="comment">//数目加一</span></span><br><span class="line">	s-&gt;count++;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//弹栈</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">pop</span><span class="params">(LinkStack *s)</span></span>&#123;</span><br><span class="line">	<span class="comment">//将栈顶弹出</span></span><br><span class="line">	LinkStackPtr topStack=s-&gt;top;</span><br><span class="line">    <span class="comment">//辅助栈</span></span><br><span class="line">	LinkStack * mins=s-&gt;mins;</span><br><span class="line">	<span class="comment">//保存辅助栈栈顶元素便于销毁</span></span><br><span class="line">	LinkStackPtr minsTopStack=mins-&gt;top;</span><br><span class="line">	s-&gt;top=topStack-&gt;next;</span><br><span class="line">	s-&gt;count--;</span><br><span class="line">	<span class="comment">//弹出的元素是最小值</span></span><br><span class="line">	<span class="keyword">if</span> (topStack-&gt;data==mins-&gt;top-&gt;data)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//辅助栈同时弹栈</span></span><br><span class="line">		mins-&gt;top=minsTopStack-&gt;next;</span><br><span class="line">		mins-&gt;count--;</span><br><span class="line">		free(minsTopStack);</span><br><span class="line">	&#125;</span><br><span class="line">	free(topStack);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//遍历栈（栈顶到栈底）</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">printStack</span><span class="params">(LinkStack *stack)</span></span>&#123;</span><br><span class="line">	<span class="comment">//当前栈顶</span></span><br><span class="line">	LinkStackPtr stackNode=stack-&gt;top;</span><br><span class="line">	<span class="keyword">do</span>&#123;</span><br><span class="line">		printf(<span class="string">&quot;%d\n&quot;</span>,stackNode-&gt;data);</span><br><span class="line">		stackNode=stackNode-&gt;next;</span><br><span class="line">	&#125;<span class="keyword">while</span>(stackNode!=NULL);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> <span class="keyword">const</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//申明并初始化栈结构</span></span><br><span class="line">	LinkStack *stack=init();</span><br><span class="line">	<span class="comment">//初始化这个链栈的辅助（C语言学的不好原谅这些很奇怪的操作）</span></span><br><span class="line">	stack-&gt;mins=init();</span><br><span class="line">	push(stack);</span><br><span class="line">	push(stack);</span><br><span class="line">	push(stack);</span><br><span class="line">	push(stack);</span><br><span class="line">	printf(<span class="string">&quot;*******进栈******\n&quot;</span>);</span><br><span class="line">	printStack(stack);</span><br><span class="line">	printf(<span class="string">&quot;*********辅助栈*******\n&quot;</span>);</span><br><span class="line">	printStack(stack-&gt;mins);</span><br><span class="line">	printf(<span class="string">&quot;*******最小值******\n&quot;</span>);</span><br><span class="line">	printf(<span class="string">&quot;%d\n&quot;</span>, stack-&gt;mins-&gt;top-&gt;data);</span><br><span class="line">	pop(stack);</span><br><span class="line">	printf(<span class="string">&quot;*******出栈******\n&quot;</span>);</span><br><span class="line">	printStack(stack);</span><br><span class="line">	printf(<span class="string">&quot;*******最小值******\n&quot;</span>);</span><br><span class="line">	printf(<span class="string">&quot;%d\n&quot;</span>, stack-&gt;mins-&gt;top-&gt;data);</span><br><span class="line">	printf(<span class="string">&quot;*********辅助栈*******\n&quot;</span>);</span><br><span class="line">	printStack(stack-&gt;mins);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>明显这个方法进栈出栈时间复杂度都是O(1)，空间复杂度相对会高一点，其实空间复杂度还可以优化，可以在辅助栈里面存索引，这样进栈时会避免存入相同的最小值，如  2 1 1 1 1 1 存到辅助栈就是  2 1 1 1 1 1 后面的1都是重复的，如果存索引就是 0 1 进栈时跟之前一样,出栈时判断索引是否和辅助栈存的索引一致，不一致就不动。这里因为这个是个链栈 ,要根据索引取值并不方便所以就不实现了.</p>
<p>这里我把代码全部贴上来了，C语言确实学的不怎么样，所以里面会有一些奇怪的操作，这个算法本身很简单但是用C语言一实现就会有一堆问题，昨天进栈的时候一个指针把辅助栈和主栈搞混了一直有bug, 今天早上上课才想起来，毕竟C语言写的少hahahaha,  所以后面我打算以后会同时用C语言和Java都实现一遍。</p>

        </div>

        
            <div class="post-copyright-info">
                <div class="article-copyright-info-container">
    <ul>
        <li>本文标题：可以获取最小值的栈</li>
        <li>本文作者：Resolmi</li>
        <li>创建时间：2018-09-27 00:00:00</li>
        <li>
            本文链接：https://imlgw.top/2018/09/27/6f1b786d/
        </li>
        <li>
            版权声明：本博客所有文章除特别声明外，均采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">BY-NC-SA</a> 许可协议。转载请注明出处！
        </li>
    </ul>
</div>

            </div>
        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                           rel="prev"
                           href="/2018/10/05/8e2bbf7a/"
                        >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">你有FreeBug么? ---&gt;Spring-solr</span>
                                <span class="post-nav-item">上一篇</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                           rel="next"
                           href="/2018/09/24/bea4831e/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">Spring-Redis遇到的bug</span>
                                <span class="post-nav-item">下一篇</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        

        
            <div class="comment-container">
                <div class="comments-container">
    <div id="comment-anchor"></div>
    <div class="comment-area-title">
        <i class="fas fa-comments">&nbsp;评论</i>
    </div>
    

        
            <section class="disqus-comments">
<div id="disqus_thread">
  <noscript>Please enable JavaScript to view the <a target="_blank" rel="noopener" href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
</section>

<script>
var disqus_shortname = 'imlgw';

var disqus_url = 'https://imlgw.top/2018/09/27/6f1b786d/';

(function(){
  var dsq = document.createElement('script');
  dsq.type = 'text/javascript';
  dsq.async = true;
  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>

<script id="dsq-count-scr" src="//imlgw.disqus.com/count.js" async></script>
        
    
</div>

            </div>
        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2018</span>&nbsp;-&nbsp;
            
            2021&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">Resolmi</a>
        </div>
        
            <script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    <span id="busuanzi_container_site_uv">
                        访问人数&nbsp;<span id="busuanzi_value_site_uv"></span>&ensp;
                    </span>
                
                
                    <span id="busuanzi_container_site_pv">
                        总访问量&nbsp;<span id="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        
            <div class="icp-info info-item"><a target="_blank" rel="nofollow" href="https://beian.miit.gov.cn">鄂ICP备18011208号</a></div>
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        
            <li class="tools-item page-aside-toggle">
                <i class="fas fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
            <li class="go-comment">
                <i class="fas fa-comment"></i>
            </li>
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
    </ul>
</div>

    </div>

    
        <aside class="page-aside">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E5%8F%AF%E4%BB%A5%E8%8E%B7%E5%8F%96%E6%9C%80%E5%B0%8F%E5%80%BC%E7%9A%84%E6%A0%88%EF%BC%9F"><span class="nav-number">1.</span> <span class="nav-text">如何实现可以获取最小值的栈？</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Solution-1%EF%BC%9A"><span class="nav-number">1.1.</span> <span class="nav-text">Solution 1：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Solution-2%EF%BC%9A"><span class="nav-number">1.2.</span> <span class="nav-text">Solution 2：</span></a></li></ol></li></ol>
    </div>
</div>
        </aside>
    

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fas fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="搜索..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fas fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fas fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>



<script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/utils.js"></script><script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/main.js"></script><script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/header-shrink.js"></script><script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/back2top.js"></script><script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/dark-light-toggle.js"></script>


    <script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/local-search.js"></script>



    <script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/code-copy.js"></script>



    <script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/lazyload.js"></script>


<div class="post-scripts pjax">
    
        <script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/left-side-toggle.js"></script><script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/libs/anime.min.js"></script><script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/toc.js"></script>
    
</div>


    <script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@3.4.3/source/js/libs/pjax.min.js"></script>
<script>
    window.addEventListener('DOMContentLoaded', () => {
        window.pjax = new Pjax({
            selectors: [
                'head title',
                '.page-container',
                '.pjax'
            ],
            history: true,
            debug: false,
            cacheBust: false,
            timeout: 0,
            analytics: false,
            currentUrlFullReload: false,
            scrollRestoration: false,
            // scrollTo: true,
        });

        document.addEventListener('pjax:send', () => {
            KEEP.utils.pjaxProgressBarStart();
        });

        document.addEventListener('pjax:complete', () => {
            KEEP.utils.pjaxProgressBarEnd();
            window.pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
            KEEP.refresh();
        });
    });
</script>



<script src="https://cdn.jsdelivr.net/npm/live2d-widget@3.x/lib/L2Dwidget.min.js"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"https://cdn.jsdelivr.net/npm/live2d-widget-model-hijiki@1.0.5/assets/hijiki.model.json"},"display":{"superSample":2,"width":160,"height":320,"position":"right","hOffset":0,"vOffset":-70},"mobile":{"show":false,"scale":0.2},"log":false});</script></body>
</html>
